import os
import json
from basic_component_related_management.models import *
from django.shortcuts import get_object_or_404
from django.db import IntegrityError, transaction

intermediate_data_dir = "./files/component_related_dataV1/Intermediate_data"
meta_data_dir = "./files/component_related_dataV1/meta_data"

# 初始化属性数据
def init_attribute_data():
    attribute_file = os.path.join(meta_data_dir, "component_attribute.json")
    with open(attribute_file, 'r', encoding='utf-8') as f:
        attribute_data = json.load(f)
    user_name = "admin"
    attribute_to_create = []
    for attribute in attribute_data:
        attribute_item = {}
        attribute_item['main_name'] = attribute['attribute_name']
        attribute_item['main_name_en'] = attribute['attribute_name_en']
        attribute_item['main_dataIndex'] = attribute['attribute_dataIndex']
        attribute_item['main_datatype'] = attribute['attribute_type']
        attribute_item['main_necessary'] = attribute['attribute_necessary']
        attribute_item['comment'] = attribute['comment']
        attribute_item['common_used'] = attribute['common_used']
        attribute_item['multiple_value_used'] = attribute['multiple_value_used']
        attribute_item['is_system_config'] = attribute['is_system_config']
        attribute_item['create_person'] = user_name
        attribute_item['modify_person'] = user_name
        attribute_to_create.append(Attribute(**attribute_item))
    
    Attribute.objects.bulk_create(attribute_to_create)

    print("init attribute data Done!")
        

# 初始化全部调用函数
def init_component_related_dataV1():
    init_attribute_data()
    
    print("init component related data Done!")